package com.techstar.api.service.city;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Arrays;
import java.util.Properties;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.util.EntityUtils;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.springframework.beans.factory.annotation.Autowired;

import com.techstar.api.service.ConfigProperties;
import com.techstar.api.service.GateWayApiProperties;
import com.techstar.api.service.HttpClientHeper;
import com.techstar.api.service.TrafficApiProperties;
import com.techstar.roma.http.GatewayService;
import com.techstar.roma.http.SHA256Util;

import lombok.extern.slf4j.Slf4j;

/**
 * 
 * @author EinIce
 * @description 接口加密校验
 * @date 2021年8月20日
 */
//@Service
@Slf4j
public class ZDemoService {

//	@Autowired
//	private FlightFlowRepos flightFlowRepos;

//	private static final ObjectMapper objectMapper = JacksonMapper.instance.objectMapper;
	
	@Autowired
	private ConfigProperties configProperties; // 配置文件注入
	
	public void query(){
		GateWayApiProperties gateWayApiProperties = configProperties.romaApiProperties();
		TrafficApiProperties trafficApiProperties = configProperties.dctApiProperties();
		if(gateWayApiProperties.testMode==true&&!"test".equals(trafficApiProperties.able)
				||"disable".equals(trafficApiProperties.able)) {
			log.info("DistrictCongestTrendService out");
			return;
		}
//		trafficApiProperties.params = String.format("dist_id=%s&end_time=%s&start_time=%s", "4553", "2359", "0000");
//		trafficApiProperties.body = "";
		HttpGet httpGet = new HttpGet();
		httpGet = (HttpGet) GatewayService.GateWay(httpGet,gateWayApiProperties, trafficApiProperties);
		CloseableHttpResponse response = null;
		String result = null;
        try {
            response = HttpClientHeper.instance.httpclient.execute(httpGet);
            HttpEntity entity = response.getEntity();
            result = EntityUtils.toString(entity);
            log.info(result);
            response.close();
        }catch (Exception e) {
        	e.printStackTrace();
		}
	}
	public static void main(String[] args) throws Exception {
		Properties properties = new Properties();
	    // 使用InPutStream流读取properties文件
	    BufferedReader bufferedReader = new BufferedReader(new FileReader("F:\\WorkSpace\\eclipse\\HT2020\\sz-traffic\\traffic-ioc\\traffic-ioc-master\\src\\main\\resources\\application.properties"));
	    properties.load(bufferedReader);
		
	    
		StringBuffer gateWayFirst = new StringBuffer();
		String url = "/index/district_trend";
		String host = properties.getProperty("traffic.roma-api.host");
		String time = DateTime.now(DateTimeZone.UTC).toString("yyyyMMdd'T'HHmmss'Z'");
		String timeTag = properties.getProperty("traffic.roma-api.timeTag");
		String heard = properties.getProperty("traffic.roma-api.heard");
		String heardSHA256 = SHA256Util.SHA256("");
		String params="dist_id=%s&end_time=%s&start_time=%s";
		params = String.format(params, "4553", "2359", "0000");
		String request = "http://%s?%s";
        HttpGet httpGet = new HttpGet(String.format(request, host+url ,params));
        
        System.out.println(httpGet.getURI());
        System.out.println("————————————————————————————————————————————————————");
        gateWayFirst.append(httpGet.getMethod()+"\n"+url+"\n"+params+"\nhost:"+host+"\n"+timeTag+time+"\n\n"+heard+"\n"+heardSHA256);
		System.out.println(gateWayFirst);
		System.out.println("————————————————————————————————————————————————————");
		String SHA256 = SHA256Util.SHA256(gateWayFirst.toString());
		System.out.println(SHA256);
		System.out.println("————————————————————————————————————————————————————");
		
		StringBuffer gateWaySecond = new StringBuffer();
		String sha256Tag = properties.getProperty("traffic.roma-api.sha256Tag");
		gateWaySecond.append(sha256Tag+"\n"+time+"\n"+SHA256);
		System.out.println(gateWaySecond);
		System.out.println("————————————————————————————————————————————————————");
		String HmacSHA256 = SHA256Util.HMACSHA256(gateWaySecond.toString(),"t0@+#3ueUf919@84i95J6tO$O=CaO50ty29Tb8e=9N#8m2NT$84-gOy.-#LTBj-@");
		System.out.println(HmacSHA256);
		
		String authorization = properties.getProperty("traffic.roma-api.authorization");
		httpGet.addHeader("X-Sdk-Date", time);
		httpGet.addHeader("host", host);
		httpGet.addHeader("Authorization", authorization+HmacSHA256);
		System.out.println(Arrays.toString(httpGet.getAllHeaders()));
	}
	public void test() {
		String url = "http://127.0.0.1:8087/terraria/user/test";
		HttpGet httpGet = new HttpGet(url);
		CloseableHttpResponse response = null;
        String result = null;
        try {
            response = HttpClientHeper.instance.httpclient.execute(httpGet);
            HttpEntity entity = response.getEntity();
            result = EntityUtils.toString(entity);
            System.out.println(result);
            response.close();
        }catch (Exception e) {
        	e.printStackTrace();
		}
	}
}